//
//  ReadViewController.m
//  Leisure
//
//  Created by 大展 on 16/6/28.
//  Copyright © 2016年 大展. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataSource;  // 数据源
@property (nonatomic, strong)NSMutableArray *scrollArray; // 轮播图图片

@end

@implementation ReadViewController

- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)scrollArray {

    if (!_scrollArray) {

        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

- (UICollectionView *)collectionView {

    if (!_collectionView) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        // 设置item大小
        layout.itemSize = CGSizeMake(100, 100);
        // 设置间距
        layout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3+64, kWidth, kHeight/2) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [RequestManager fecthDataWithUrl:kReadUrl parameter:@{} mehtod:@"GET" compelet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        // 添加轮播图的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {

            [self.scrollArray addObject:smallDic[@"img"]];
        }
        // 添加collectionView的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {

            ReadModel *model = [[ReadModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
        } updateUI:^{

            // 创建轮播图
            ZZScrollView *scrollView = [[ZZScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3) imageArray:self.scrollArray];
            [self.view addSubview:scrollView];
            [self.collectionView reloadData];
    }];

    // 注册XIB
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"read_cell"];

    
    [self.view addSubview:self.collectionView];

    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark ------collectionViewDelegate------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
    // 设置cell
    [cell setCellWithModel:self.dataSource[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    // 通过Xib来获取控制器
    ReadListViewController *readListVC = [[ReadListViewController alloc]initWithNibName:@"ReadListViewController" bundle:nil];

    ReadModel *model = self.dataSource[indexPath.row];
    readListVC.typeID = model.type;

    [self.navigationController pushViewController:readListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
